﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SMP.Client.MasterForm
{
    using System.Collections;
    using SMP.Windows;
    using SMP.Service;

    public partial class FrmNhom_Mathang : SMP.Windows.Forms.FrmBase
    {
        Mathang_Service _objMathang = new Mathang_Service();
        SMP.Domain.Nhom_Mathang nhom_mathang = new Domain.Nhom_Mathang();
        DataTable dtb_nhom_mathang = new DataTable();

        public FrmNhom_Mathang()
        {
            InitializeComponent();
            item_Print.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        #region DisplayInfo, ResetText, ChangeStatus, ClearDataBindings, DataBindingControl

        public override void DisplayInfo()
        {
            try
            {
                this.FormState = Windows.Forms.FormState.View;
                dtb_nhom_mathang = _objMathang.GetAll_Nhom_Mathang();
                dgNhom_Mathang.DataSource = dtb_nhom_mathang;
                                
                ClearDataBindings();
                ResetText();
                ChangeStatus(true);
                if (gvNhom_Mathang.RowCount > 0)
                {
                    DataBindingControl();
                    item_Select.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                }
                else
                    item_Select.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            }
            catch (Exception ex)
            {
                Windows.Forms.Message.Show(ex.Message, ex.ToString());
            }
        }

        public override void ResetText()
        {
            txtNhom_Mathang.EditValue = null;
            txtNhom_Mathang.Focus();
        }

        public override void ChangeStatus(bool editTable = true)
        {
            txtNhom_Mathang.Properties.ReadOnly = editTable;
        }

        public override void ClearDataBindings()
        {
            txtNhom_Mathang.DataBindings.Clear();
        }

        public override void DataBindingControl()
        {
            txtNhom_Mathang.DataBindings.Add("EditValue", dtb_nhom_mathang, ".Ten_Nhom_Mathang");            
        }

        #endregion


        #region PerformAdd, PerformEdit, PerformDelete, PerformCancel, PerformSave

        public override bool PerformAdd()
        {
            ClearDataBindings();
            ResetText();
            ChangeStatus(false);
            return true;
        }

        public override bool PerformEdit()
        {
            ChangeStatus(false);
            return true;
        }

        public override bool PerformDelete()
        {
            var del = false;
            if (_objMathang.Get_Mathang_Check_Used_By_Id_Nhom_Mathang(gvNhom_Mathang.GetFocusedRowCellValue("Id_Nhom_Mathang")))
                Windows.Forms.Message.Show("Nhóm mặt hàng đã sử dụng, không thể xóa", "Thông Báo");

            if (Windows.Forms.Message.Show("Bạn có thực sự muốn xóa " + txtNhom_Mathang.EditValue + " ", "Thông Báo", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
               del = _objMathang.Delete_Nhom_Mathang(gvNhom_Mathang.GetFocusedRowCellValue("Id_Nhom_Mathang"));
            if (del)
                DisplayInfo();
            return del;
        }

        public override bool PerformCancel()
        {
            ClearDataBindings();
            DisplayInfo();
            return true;
        }

        public override bool PerformSave()
        {
             var save = false;
            Hashtable hashtableControl = new Hashtable();
            hashtableControl.Add(txtNhom_Mathang, lbNhom_Mathang);
            if (!Windows.Utility.Validator.CheckNullFields(hashtableControl))
                return false;
            if (dtb_nhom_mathang != null && dtb_nhom_mathang.Rows.Count > 0)
            {
                DataRow[] row = dtb_nhom_mathang.Select(string.Format("Ten_Nhom_Mathang = '{0}'", txtNhom_Mathang.EditValue));
                if (row.Length > 0)
                {
                    Windows.Forms.Message.Show("Nhóm mặt hàng "+txtNhom_Mathang.EditValue + " đã tồn tại.\nVui lòng xem lại", "Thông Báo", MessageBoxButtons.OK);
                    return false;
                }
            }
            nhom_mathang.Ten_Nhom_Mathang = txtNhom_Mathang.EditValue;
            if (this.FormState == Windows.Forms.FormState.Add)
                save = _objMathang.Insert_Nhom_Mathang(nhom_mathang);
            else
                save = _objMathang.Update_Nhom_Mathang(nhom_mathang);
            if (save)
                DisplayInfo();


            return save;
        }

        public DataRow SelectedRow { set; get; }
        public override object PerformSelectOneObject()
        {
            SelectedRow = gvNhom_Mathang.GetDataRow(gvNhom_Mathang.FocusedRowHandle);

            if (SelectedRow == null)
            {
                Windows.Forms.Message.Show("Chưa chọn Nhóm mặt hàng.\nVui lòng chọn mặt hàng", "Thông Báo", MessageBoxButtons.OK);
                return null;
            }
            this.Close();
            this.Dispose();
            return true;
        }


        #endregion










    }
}